-- Viral Resistance - T-Engine 4 Module
-- Copyright (C) 2010 Mikolai Fajer
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program.  If not, see <http://www.gnu.org/licenses/>.
--
-- Mikolai Fajer "yufra"
-- mfajer@gmail.com

require "mod.dialogs.SelectMultipleFromInventory"

local barter_answer = {[["Do have any weapons or ammo to spare?"]],
	action = function(npc, player)
		if player.vload < 50 then
			return "barter"
		else
			return "barter_sick"
		end
	end,
}

newChat{ id = "first",
	text = [[#LIGHT_GREEN#A shotgun greets you over a barrier of chairs thrown on the top of desks.  #WHITE#"Keep back, keep back!"]],
	answers = {
		{[["What happened to the power?"]],
			jump = "power",
			cond=function(npc, player)
				return player:isQuestStatus("power_loss", engine.Quest.PENDING, "investigate")
			end,
		},
		barter_answer,
		{"#LIGHT_BLUE#Walk away."},
	}
}

newChat{ id = "power",
	text = [[#LIGHT_GREEN#A terror-stricken face peers through a gap in the barrier.  #WHITE#"We don't know, we saw a decontamination lockdown in the Biohazard Ward and a few minutes later everything goes crazy.  The emergency generators should have kicked in and we sent a man down to investigate but the he hasn't come back yet."]],
	answers = {
		barter_answer,
		{"#LIGHT_BLUE#Walk away."},
	}
}

local count_meds= function(player)
	local num_meds = 0
	for inven_id, inven in pairs(player.inven) do
		for item, o in ipairs(inven) do
			if (o.type == "medicine") then
				num_meds = num_meds + o:getNumber()
			end
		end
	end
	return num_meds
end

local collect_meds = function(player, num, reward_func)
	local num_collected = 0
	local dialog = mod.dialogs.SelectMultipleFromInventory.new("Offer which medicine?",
		player:getInven(player.INVEN_INVEN),
		function(obj, idx) return obj.type == "medicine" end,
		function(obj, idx)
			game.log("You hand over %s.", obj:getName{no_count=true, do_color=true})
			player:removeObject(player.INVEN_INVEN, idx)
			num_collected = num_collected + 1
			-- Give the gun and exit the dialog
			if num_collected == num then
				reward_func(player)
				return true
			end
		end,
		player
	)
	game:registerDialog(dialog)
end

newChat{ id = "barter",
	text = [[#WHITE#"If you have any medical supplies we will take those, but we aren't going to give up our ammo for nothing.  We also have some pistols and even a few shotguns, but those are going to cost you."]],
	answers = {
		{[[#LIGHT_BLUE#Offer 1 medication for some ammo (tries to match your current gun).]],
			cond = function(npc, player)
				return count_meds(player) >= 1
			end,
			action = function(npc, player)
				local reward = function(player)
					local weapon = player:getInven(player.INVEN_MAINHAND)[1]
					local weapon_type = weapon and weapon.type == "firearm" and weapon.subtype
					local ammo = game.zone:makeEntity(game.level, "object", {type="ammo", subtype=weapon_type}, nil, true)
					player:addObject(player.INVEN_INVEN, ammo)
					game.log("A %s is pushed through the barrier.", ammo:getName{no_count=true, do_color=true})
				end
				collect_meds(player, 1, reward)
				return "finished barter"
			end,
		},
		{[[#LIGHT_BLUE#Offer 4 medications for a pistol.]],
			cond = function(npc, player)
				return count_meds(player) >= 4
			end,
			action = function(npc, player)
				local reward = function(player)
					local pistol = game.zone:makeEntity(game.level, "object", {type="firearm", subtype="pistol"}, nil, true)
					player:addObject(player.INVEN_INVEN, pistol)
					game.log("A %s is pushed through the barrier.", pistol:getName{no_count=true, do_color=true})
				end
				collect_meds(player, 4, reward)
				return "finished barter"
			end,
		},
		{[[#LIGHT_BLUE#Offer 10 medications for a shotgun.]],
			cond = function(npc, player)
				return count_meds(player) >= 10
			end,
			action = function(npc, player)
				local reward = function(player)
					local shotgun = game.zone:makeEntity(game.level, "object", {type="firearm", subtype="shotgun"}, nil, true)
					player:addObject(player.INVEN_INVEN, shotgun)
					game.log("A %s is pushed through the barrier.", shotgun:getName{no_count=true, do_color=true})
				end
				collect_meds(player, 10, reward)
				return "finished barter"
			end,
		},
		{[["No thanks."]], jump = "decline barter",},
	},
}

newChat{ id = "finished barter",
	text = [[#WHITE#"Come back if you find any more medical supplies, it might be a while before the cavalry arrives and it pays to be prepared."]],
	answers = {
		{[["Wait, I have some more."]], jump = "barter"},
		{"#LIGHT_BLUE#Walk away."},
	},
}

newChat{ id = "decline barter",
	text = [[#WHITE#"Well don't waste our time then, get lost."]],
	answers = {
		{"#LIGHT_BLUE#Walk away."},
	},
}

newChat{ id = "barter_sick",
	text = [[#LIGHT_GREEN#The man looks you up and down and then shakes his head.  #WHITE#"You look sick, like the others before they go crazy.  Keep back or we will shoot!"]],
	answers = {
		{"#LIGHT_BLUE#Walk away."},
	}
}

return "first"